<?php

namespace addons\facrm\controller\facrm;

use addons\facrm\library\BackendApi;
use think\Db;


/**
 * 操作记录
 */
class Operatelog extends BackendApi
{

    protected $model = null;
    protected $noNeedRight = ['*'];
    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'content';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('\app\admin\model\facrm\Operatelog');
        $this->request->filter(['strip_tags']);
    }

    /**
     * 查看
     * @ApiSummary (参数根据实际需要传，不需要三个都一起传)
     * @ApiMethod (GET)
     * @ApiParams (name="customer_id", type="int", required=false, description="客户ID")
     * @ApiParams (name="contract_id", type="int", required=false, description="合同ID")
     * @ApiParams (name="receivable_id", type="int", required=false, description="回款ID")
     * @ApiBody ("有分页")
     */
    public function index($customer_id = null, $contract_id = null, $receivable_id = null)
    {
        $customer_id = $customer_id ? $customer_id : $this->request->param("customer_id");
        $contract_id = $contract_id ? $contract_id : $this->request->param("contract_id");
        $receivable_id = $receivable_id ? $receivable_id : $this->request->param("receivable_id");


        $filter_w = [];
        if ($customer_id) {
            $filter_w['customer_id'] = $customer_id;
        }
        if ($contract_id) {
            $filter_w['contract_id'] = $contract_id;
        }
        if ($receivable_id) {
            $filter_w['receivable_id'] = $receivable_id;
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $total = $this->model
            ->where($where)
            ->where($filter_w)
            ->order($sort, $order)->fetchSql(false)
            ->count();
        $list = $this->model
            ->where($filter_w)
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)->fetchSql(false)
            ->select();
        foreach ($list as $row) {
            $row->hidden(['content']);
        }
        $result = array("total" => $total, "rows" => $list);
        return $this->success('', $result);
    }


    /**
     * 日志详情
     * @ApiParams (name="ids", type="int", required=true, description="日志ID")
     */
    public function detail($ids = NULL)
    {
        $ids = $ids ? $ids : $this->request->param("ids");
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        return $this->success('', $row->toArray());
    }


    /**
     * @return void
     * @throws 全部商品
     */
    public function test(){


        $list=Db::name('facrm_product')->where(['status'=>1])->select();

        return $this->success('test',$list);
    }


    /**
     * @return void
     * @throws
     */
    public function test1(){

        $id=$this->request->param('id');

        $find=Db::name('facrm_customer')->where(['id'=>$id])->find();


        return $this->success('商品信息',$find);
    }


    /**
     * @return void
     */
    public function test2(){

        $info=$this->request->param('');

        $admin=$this->auth->getAdmin();


        $uid=$admin['id'];

        $log['admin_id']=$uid;
        $log['customer_id']=$info['id'];
        $log['goods_id']=$info['good_id'];
        $log['number']=$info['number'];


        $log['timestamp']=time();
        $log['ordersn']='sn'.date('ymdhis').rand(10,99);
        $log['goin']=$uid;
        $log['address']=$info['address'];

        $good=Db::name('facrm_product')->where(['id'=>$info['good_id']])->find();



        foreach ($log as $kv=>$v){

            if (empty($v)){
                return $this->error('请填写完整信息'.$kv);
            }

        }

        $log['oldmoney']=$good['price']*$info['number'];

        $log['desc']=$info['desc'];

        $log['comeway']=$info['comeway'];

        $log['money']=$info['money']?$info['money']:'0';

        Db::name('order')->data($log)->insert();


        return $this->success('提交信息',$uid);


    }



    /**
     *  不同身份获取相关订单
     */
    public function test3(){

        $admin=$this->auth->getAdmin();
        $adminid=$admin['id'];
        $infos=Db::name('auth_group_access')->where(['uid'=>$adminid])->find();
        $types=$infos['group_id'];  //1 最高 2 员工 3管理
        $i=$this->request->param('i');

        $map['id']=['gt',0];
        if ($i=='-1'){

            $map['id']=['gt',0];
        }else if ($i=='0'){

            $map['status']=['eq',0];
        }else if ($i=='1'){

            $map['status']=['eq',1];
        }



        if ($types==3){
            $adminids=$adminid.',';
            $infos=Db::name('admin')->where('pid','eq',$adminids)

                ->field('id')->select();
            foreach ($infos as $v){
                $adminids.=$v['id'].',';
            }

            $list=Db::name('order')->where('admin_id','in',$adminids)
                ->where($map)->order('id desc')->select();
        }

        if ($types==1){
            $list=Db::name('order')->where('id','gt',0)
                ->where($map)
                ->order('id desc')->select();
        }


        if ($types==2){
            $list=Db::name('order')->where('admin_id','eq',$adminid)
                ->where($map)
                ->order('id desc')->select();
        }




        foreach ($list as &$kv){

            $use=Db::name('facrm_customer')->where(['id'=>$kv['customer_id']])->find();
            $kv['uname']=$use['name'];
            $kv['mobile']=$use['mobile'];


            $good=Db::name('facrm_product')->where(['id'=>$kv['goods_id']])->find();
            $kv['gname']=$good['name'];

            $ur=Db::name('facrm_customer')->where(['id'=>$kv['customer_id']])->find();
            $kv['bname']=$ur['name'];

            $kv['timestamp']=date('Y-m-d H:i',$kv['timestamp']);


            $kv['btypes']=$types==1?'a':'b';
        }

        return $this->success('订单信息',$list);


    }

    /**
     * @return 我的订单
     */
    public function test4(){


        $id=$this->request->param('id');



        $list=Db::name('order')->where(['customer_id'=>$id])->order('id desc')->select();


        foreach ($list as &$kv){

            $use=Db::name('facrm_customer')->where(['id'=>$kv['customer_id']])->find();
            $kv['uname']=$use['name'];
            $kv['mobile']=$use['mobile'];


            $good=Db::name('facrm_product')->where(['id'=>$kv['goods_id']])->find();
            $kv['gname']=$good['name'];

            $kv['timestamp']=date('Y-m-d H:i',$kv['timestamp']);
        }

        return $this->success('订单信息',$list);


    }

    /**
     * @return 删除订单
     */
    public function test5(){

        $id=$this->request->param('id');

        $admin=$this->auth->getAdmin();

        $adminid=$admin['id'];

        $infos=Db::name('auth_group_access')->where(['uid'=>$adminid])->find();


        $types=$infos['group_id'];  //1 最高 2 员工 3管理


//
//        if  ($types==2){
//            return $this->error(__("无权限"));
//        }


        Db::name('order')->where(['id'=>$id])->delete();
        return $this->success('删除订单');

    }


    /**
     * @return 用户身份
     */
    public function test6(){

        $admin=$this->auth->getAdmin();

        $adminid=$admin['id'];

        $infos=Db::name('auth_group_access')->where(['uid'=>$adminid])->find();


        $types=$infos['group_id'];  //1 最高 2 员工 3管理


        return $this->success('用户身份',$types);

    }


    /**
     * @return 审核订单
     */
    public function test7(){

        $id=$this->request->param('id');
        Db::name('order')->where(['id'=>$id])->update(['status'=>1]);
        return $this->success('删除订单');

    }

    /**
     *  全部线索
     */
    public function test8(){


        $lists=Db::name('thread')->select();

        foreach ($lists as &$key){

            $key['select']=0;

        }

        return $this->success('全部线索',$lists);

    }

    /**
     *  删除信息
     */
    public function test9(){

        $id=$this->request->param('id');

        Db::name('thread')->where(['id'=>$id])->delete();

        return $this->success('删除信息');

    }

    /**
     * @return 选择用户
     */
    public function test10(){



        $alluser=Db::name('admin')->where('id','neq',1)->select();


        $all=Db::name('admin')
            ->alias('a')
            ->join('fa_auth_group_access b','a.id=b.uid')
            ->where('a.id','neq',1)
            ->where('b.group_id','eq',3)
            ->select();


        return $this->success('选择用户',['alluser'=>$alluser,'all'=>$all]);
    }


    /**
     * @return 选择用户
     */
    public function test11(){



        $ids=$this->request->param('ids');

        $adminida=$this->request->param('uid');  //部门id

        $types_t=$this->request->param('types_t');


        $types=0;
        if ($types_t=='部门'){
            $types=1;
        }

        if ($types_t=='个人'){
            $types=2;
        }

        if ($types==0){
            $this->error('请选择');
        }

        if (intval($adminida)<1){
            $this->error('请选择');
        }


        $listst=Db::name('thread')->where('id','in',trim($ids,','))->select();


        if ($types==1){
            //平分到

            $infos=Db::name('admin')->where('pid','eq',$adminida)->field('id')->select();

            $infos[]['id']=$adminida;

            $all=count($infos);


            foreach ($listst as $ke=>$key){

                $vs= ceil($ke % $all);

                $inserts['name']=$key['name'];
                $inserts['mobile']=$key['mobile'];
                $inserts['detail_address']=$key['detail_address'];
                $inserts['remark']=$key['remark'];
                $inserts['owner_user_id']=$infos[$vs]['id'];
                $inserts['create_user_id']=1;
                $inserts['create_time']=time();
                $inserts['update_time']=time();
                $inserts['purchase_total']=$key['money'];
                $inserts['remark']=$key['remark'];
                $inserts['purchase_times']=$key['number'];
                $inserts['tags']=$key['from'];
                Db::name('facrm_customer')->data($inserts)->insert();
                Db::name('thread')->where('id','eq',$key['id'])->delete();
            }

        }


        if ($types==2){

            $user=Db::name('admin')->where(['id'=>$adminida])->find();
            if (!$user) {
                $this->error('无用户');
            }

            foreach ($listst as $key){
                $inserts['name']=$key['name'];
                $inserts['mobile']=$key['mobile'];
                $inserts['detail_address']=$key['detail_address'];
                $inserts['remark']=$key['remark'];
                $inserts['owner_user_id']=$adminida;
                $inserts['create_user_id']=1;
                $inserts['create_time']=time();
                $inserts['update_time']=time();
                $inserts['purchase_total']=$key['money'];
                $inserts['remark']=$key['remark'];
                $inserts['purchase_times']=$key['number'];
                $inserts['tags']=$key['from'];


                Db::name('facrm_customer')->data($inserts)->insert();
                Db::name('thread')->where('id','eq',$key['id'])->delete();
            }

        }




        return $this->success('操作成功');
    }

}
